# Load models
load("Data/exp_1/slopes.Rdata")
load("Data/exp_2/slopes.Rdata")
load("Data/exp_3/slopes.Rdata")

####################
### EXP. 1 TABLE ###
####################

# Clean up slopes data
exp1_slopes <- exp1_slopes %>%
    select( # Rearrange columns
        inparty_in_treatment, dv, estimate, std.error
    ) %>%
    rename(
        Beta = estimate,
        SE = std.error,
        `Dependent Variable` = dv,
        `Treatment Party` = inparty_in_treatment
    ) %>%
    mutate(
        `Treatment Party` = case_when(
            `Treatment Party` == 1 ~ "In-Party",
            `Treatment Party` == 0 ~ "Out-Party"
        )
    )

# Generate table
exp1_slopes %>%
    datasummary_df(
        title = "Group-Average Marginal Effects of Correction (Experiment 1)",
        notes = "Note: Estimates come from a model with the following form:
        Dependent Variable = Treatment * Treatment Party. n = 1,641.",
        output = "Tables/exp1_regression.txt"
    )

####################
### EXP. 2 TABLE ###
####################

# Combine slopes
exp2_slopes <- bind_rows(
    exp2_in_therm_slopes,
    exp2_out_therm_slopes,
    exp2_violence_slopes,
    exp2_norms_slopes
) %>%
    select(
        estimate, std.error, fact_type, inparty_in_treatment, voter_or_politician,
        dv
    )

# Clean up slopes data
exp2_slopes <- exp2_slopes %>%
    select( # Rearrange columns
        fact_type, voter_or_politician, inparty_in_treatment, dv, estimate, std.error
    ) %>%
    rename(
        Beta = estimate,
        SE = std.error,
        `Statistic Type` = fact_type,
        `Poll Subject's Role` = voter_or_politician,
        `Dependent Variable` = dv,
        `Poll Party` = inparty_in_treatment
    ) %>%
    mutate(
        `Statistic Type` = case_when(
            `Statistic Type` == "policy" ~ "Policy Preference",
            `Statistic Type` == "dem_norm" ~ "Support for Undemocratic Practice",
            `Statistic Type` == "demo" ~ "Demographic"
        ),
        `Poll Subject's Role` = case_when(
            `Poll Subject's Role` == "voters" ~ "Voters",
            `Poll Subject's Role` == "politicians" ~ "Politicians"
        ),
        `Dependent Variable` = case_when(
            `Dependent Variable` == "out_therm_post" ~ "Out-Party Warmth",
            `Dependent Variable` == "in_therm_post" ~ "In-Party Warmth",
            `Dependent Variable` == "norms_post" ~ "Support for Undemocratic Practices",
            `Dependent Variable` == "violence_post" ~ "Support for Partisan Violence"
        ),
        `Poll Party` = case_when(
            `Poll Party` == 1 ~ "In-Party",
            `Poll Party` == 0 ~ "Out-Party"
        )
    )

# Generate table
exp2_slopes %>%
    datasummary_df(
        title = "Group-Average Marginal Effects of Moving Statistic One
        Percentage Point in Direction of Party Stereotype (Experiment 2)",
        notes = "Note: Estimates come from a model with the following form:
        Post-Treatment Dependent Variable = Deviation of Statistic from
        Participant's Prior * Statistic Type * Poll Subject's Role * Poll Party
        + Participant's Prior. Standard errors are clustered at the participant
        level. n = 1,642.",
        output = "Tables/exp2_regression.txt"
    )

####################
### EXP. 3 TABLE ###
####################

# Combine slopes
exp3_slopes <- bind_rows(
    exp3_in_therm_slopes,
    exp3_out_therm_slopes,
    exp3_violence_slopes,
    exp3_norms_slopes
) %>%
    select(
        estimate, std.error, fact_type, inparty_in_treatment, voter_or_politician,
        dv
    )

# Clean up slopes data
exp3_slopes <- exp3_slopes %>%
    select( # Rearrange columns
        fact_type, voter_or_politician, inparty_in_treatment, dv, estimate, std.error
    ) %>%
    rename(
        Beta = estimate,
        SE = std.error,
        `Statistic Type` = fact_type,
        `Poll Subject's Role` = voter_or_politician,
        `Dependent Variable` = dv,
        `Poll Party` = inparty_in_treatment
    ) %>%
    mutate(
        `Statistic Type` = case_when(
            `Statistic Type` == "policy" ~ "Policy Preference",
            `Statistic Type` == "dem_norm" ~ "Support for Undemocratic Practice",
            `Statistic Type` == "demo" ~ "Demographic"
        ),
        `Poll Subject's Role` = case_when(
            `Poll Subject's Role` == "voters" ~ "Voters",
            `Poll Subject's Role` == "politicians" ~ "Politicians"
        ),
        `Dependent Variable` = case_when(
            `Dependent Variable` == "out_therm_post" ~ "Out-Party Warmth",
            `Dependent Variable` == "in_therm_post" ~ "In-Party Warmth",
            `Dependent Variable` == "norms_post" ~ "Support for Undemocratic Practices",
            `Dependent Variable` == "violence_post" ~ "Support for Partisan Violence"
        ),
        `Poll Party` = case_when(
            `Poll Party` == 1 ~ "In-Party",
            `Poll Party` == 0 ~ "Out-Party"
        )
    )

# Generate table
exp3_slopes %>%
    datasummary_df(
        title = "Group-Average Marginal Effects of Moving Statistic One
        Percentage Point in Direction of Party Stereotype (Experiment 3)",
        notes = "Note: Estimates come from a model with the following form:
        Post-Treatment Dependent Variable = Deviation of Statistic from
        Participant's Prior * Statistic Type * Poll Subject's Role * Poll Party
        + Participant's Prior. n = 2,484.",
        output = "Tables/exp3_regression.txt"
    )
